#include "SendEmail.h"

CSendEmail::CSendEmail(void)
{
	m_Sender = "Default@google.com";
	m_SenderName = "Default";
	m_Subject = "hello friend";
	m_Text = "i love you";
}

CSendEmail::~CSendEmail(void)
{
}

void CSendEmail::SetSenderInfo(CString Sender, CString SenderName)
{

	if (Sender != "")
	{
		m_Sender = Sender;
	}

	if (SenderName != "")
	{
		m_SenderName = SenderName;
	}
}

void CSendEmail::SetRecipientInfo(CString Recipient)
{

	if (Recipient != "")
	{
		m_Recipient = Recipient;
		CString temp;
		int pos;
		pos = Recipient.Find('@');
			m_ReceiveServer = Recipient.Right(Recipient.GetLength() - pos -1);
	}else
	{
		MessageBox(NULL,"Recipient error!","",MB_OK);
		return;
	}
}

void CSendEmail::SetMailInfo(CString Subject, CString Text)
{

	if (Subject != "")
	{
		m_Subject = Subject;
	}

	if (Text != "")
	{
		m_Text = Text;
	}
}

void CSendEmail::AddAttachFile(CString AttachFile)
{

	m_AttachFileList.AddTail(AttachFile);
}

int CSendEmail::SendMail()
{
	if (m_Recipient == "")
	{
		//	errorResult = "null Recipient";
		return 2;
	}

	::CoInitialize(NULL); 
	CMimeMessage msg;
	msg.SetSender(m_Sender);	

	msg.SetSenderName(m_SenderName);
	msg.AddRecipient(m_Recipient);	

	msg.SetSubject(m_Subject);
	msg.AddText(m_Text);
	POSITION pos = m_AttachFileList.GetHeadPosition();
	while(pos!=NULL)
	{
		msg.AttachFile(m_AttachFileList.GetNext(pos));
	}

	CSMTPConnection2 conn;

	if(conn.Connect(m_ReceiveServer))
	{
		if( conn.SendMessage(msg) == TRUE )
		{
			return 1;
		}
		else
		{
			//	errorResult = "fail!";
		//	::CoUninitialize();
			return 3;
		}
		conn.Disconnect();    
	}
	else
	{
		//	errorResult = "UnConnected!";
	//	::CoUninitialize();
		return 4;
	}
	::CoUninitialize();
	return 1;
}
